﻿using System;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Dynamic;
using WebDKMH.Models;
using CaptchaMvc.Attributes;
using CaptchaMvc.HtmlHelpers;
using CaptchaMvc.Interface;

namespace WebDKMH.Controllers
{
    public class TrangchuController : Controller
    {
        //
        // GET: /Trangchu/
        dangkymonhocEntities2 db = new dangkymonhocEntities2();
        public ActionResult Index()
        {
            var trangchu = (from s in db.thongbao1 where s.kichhoat==true select  s   ).ToList();
            return View(trangchu);
        }
        public ActionResult Chitietthongbao(int id2)
        {
            var tb =(from d in db.thongbao1
                                where d.id == id2 
                                select d).First();
            return View(tb);
        }


        public ActionResult Thongbao()
        {
            var thongbao = from s in db.thongbao1 select s;
            return View(thongbao.ToList());
        }

       
        //Module hiển thị kế hoạch đào tạo
        public ActionResult ChonGV()
        {

            DateTime date=DateTime.Now;
            int snamhoc2 = date.Year;
            int snamhoc1 = snamhoc2 - 1;
            string snamhoc = "";
            string shocky = "";
            int sthang = date.Month;
            if (sthang >= 4 && sthang <= 7)
            {
                snamhoc = Convert.ToString(snamhoc2).Trim();
                shocky = "1";
            }
            else
            {
                snamhoc = Convert.ToString(snamhoc1).Trim();
                shocky = "2";
            }
            string smalop = Session["malop"].ToString();
            string smasv = Session["LoginUserID"].ToString();
            var query = from kh in db.kehoachdaotaos
                        join sv in db.sinhviens on kh.malop equals sv.malop
                        join l in db.lops on sv.malop equals l.malop
                        where sv.masv.Trim() == smasv.Trim() & kh.namhoc.Trim() == snamhoc & kh.hocky.Trim() == shocky
                        select kh;
            return View(query);
        }


        [HttpPost]
        public ActionResult ChonGV(FormCollection col, dangkymonhoc dk, tinhtrangdangky tr, string empty)
        {
            if (this.IsCaptchaValid("")==false)
            {
                TempData["ErrorMessage"] = "Mã nhập không chính xác, vui lòng nhập lại.";
                return RedirectToAction("ChonGV");

            }
            else
            {
                    string smasv = Session["LoginUserID"].ToString();
                    string smalop = Session["malop"].ToString();
                    DateTime date = DateTime.Now;
                    bool state = false;
                    int snamhoc2 = date.Year;
                    int snamhoc1 = snamhoc2 - 1;
                    string snamhoc = "";
                    string shocky = "";
                    int sthang = date.Month;
                    if (sthang >= 4 && sthang <= 7)
                    {
                        snamhoc = Convert.ToString(snamhoc2).Trim();
                        shocky = "1";
                    }
                    else
                    {
                        snamhoc = Convert.ToString(snamhoc1).Trim();
                        shocky = "2";
                    }            
            
                    int n = Convert.ToInt32(col["soluongmh"]);
                    bool ktranull = false;
                    for (int i = 1; i <= n; i++)
                    {
                        //string smamhgv = col["gvchon" + i].Trim();
                        if (col["gvchon" + i] == null)
                        {
                            ktranull = true;
                            break;
                        }
                    }
                    if (ktranull ==false)
                    {    for (int i = 1; i <= n; i++)
                            {
                                string smamhgv = col["gvchon" + i].Trim();
                                int vttru = smamhgv.IndexOf("-");
                                int dodai = smamhgv.Length;
                                string sgvchon = smamhgv.Substring(0, vttru);
                                string smamh = smamhgv.Substring(vttru + 1);

                                dk.malop = smalop;
                                dk.namhoc = snamhoc;
                                dk.hocky = shocky;
                                dk.masv = smasv;
                                dk.mamh = smamh;
                                dk.magv = sgvchon;
                                db.dangkymonhocs.Add(dk);
                                db.SaveChanges();
                            }
                            tr.tinhtrangdk= !state;
                            tr.thoigiandk = date;
                            tr.namhoc= snamhoc;
                            tr.hocky = shocky;
                            tr.masv = smasv;
                            db.tinhtrangdangkies.Add(tr);
                            db.SaveChanges();
                            return RedirectToAction("Ketquadangky");

                    }
                    else
                            return RedirectToAction("ChonGV");           
               }
        }
        public ActionResult Ketquadangky()
        {
            DateTime date = DateTime.Now;
            int snamhoc2 = date.Year;
            int snamhoc1 = snamhoc2 - 1;
            string snamhoc = "";
            string shocky = "";
            int sthang = date.Month;
            if (sthang >= 4 && sthang <= 7)
            {
                snamhoc = Convert.ToString(snamhoc2).Trim();
                shocky = "1";
            }
            else
            {
                snamhoc = Convert.ToString(snamhoc1).Trim();
                shocky = "2";
            }
            string smalop = Session["malop"].ToString();
            
            string smasv=Session["LoginUserID"].ToString();
            IEnumerable<MyViewModel> viewModel = null;
            viewModel = from dk in db.dangkymonhocs
                            join gv in db.giaoviens on dk.magv equals gv.magv
                            join kh in db.kehoachdaotaos on dk.mamh equals kh.mamh                       
                            where dk.masv.Trim() == smasv.Trim() & kh.namhoc.Trim() == snamhoc & kh.hocky.Trim() == shocky
                            select new MyViewModel { sotc = kh.sotinchi, tengv=gv.hotengv, tenmh=kh.tenmh, magv = gv.magv, mamh=kh.mamh };
            return View(viewModel);

        }

        
        public ActionResult Test()
        {
            
            DateTime date = DateTime.Now;
            int snamhoc2 = date.Year;
            int snamhoc1 = snamhoc2 - 1;
            string snamhoc = "";
            string shocky = "";
            int sthang = date.Month;
            if (sthang >= 4 && sthang <= 7)
            {
                snamhoc = Convert.ToString(snamhoc2).Trim();
                shocky = "1";
            }
            else
            {
                snamhoc = Convert.ToString(snamhoc1).Trim();
                shocky = "2";
            }
            string smasv = Session["LoginUserID"].ToString();
            var test = from t in db.tinhtrangdangkies
                       where t.masv.Trim() == smasv.Trim() && t.namhoc.Trim() == snamhoc & t.hocky.Trim() == shocky
                       select t;
            if (test.Count() != 0)
            {
                return RedirectToAction("Baoloi1");
            }
            else
                return RedirectToAction("Test2");
            
          
        }
        public ActionResult Baoloi1()
        {
            return View();
        }
        public ActionResult Test2()
        {
            DateTime date = DateTime.Now;
            string stenkhoa = Session["tenkhoa"].ToString();
            var timetest = from tt in db.thoigiandks where tt.tenkhoa == stenkhoa && tt.ngaybatdau <= date && tt.ngayketthuc >= date select tt;
            if (timetest.Count() == 0)
            {
                return RedirectToAction("Baoloi2");
            }
            else
                return RedirectToAction("ChonGV");
        }
        public ActionResult Baoloi2()
        {
            return View();
        }
        public ActionResult Chonlai()
        {
            DateTime date = DateTime.Now;
            int snamhoc2 = date.Year;
            int snamhoc1 = snamhoc2 - 1;
            string snamhoc = "";
            string shocky = "";
            int sthang = date.Month;
            if (sthang >= 4 && sthang <= 7)
            {
                snamhoc = Convert.ToString(snamhoc2).Trim();
                shocky = "1";
            }
            else
            {
                snamhoc = Convert.ToString(snamhoc1).Trim();
                shocky = "2";
            }
            string smalop = Session["malop"].ToString();
            string smasv = Session["LoginUserID"].ToString();
            IEnumerable<MyViewModel> viewModel = null;
            viewModel = from kh in db.kehoachdaotaos
                        join sv in db.sinhviens on kh.malop equals sv.malop
                        join l in db.lops on sv.malop equals l.malop
                        join dk in db.dangkymonhocs on kh.mamh equals dk.mamh
                        join gv in db.giaoviens on dk.magv equals gv.magv
                        where sv.masv.Trim() == smasv.Trim() & kh.namhoc.Trim() == snamhoc & kh.hocky.Trim() == shocky
                        select new MyViewModel { mamh = kh.mamh, tenmh = kh.tenmh, sotc = kh.sotinchi, magv1 = kh.magv1, magv2 = kh.magv2, magv3 = kh.magv3, tengv1 = kh.tengv1, tengv2 = kh.tengv2, tengv3 = kh.tengv3,tengvchon=gv.hotengv,id=dk.id };
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Chonlai(FormCollection col)
        {

            string smasv = Session["LoginUserID"].ToString();
            string smalop = Session["malop"].ToString();
            DateTime date = DateTime.Now;
            bool state = false;
            int snamhoc2 = date.Year;
            int snamhoc1 = snamhoc2 - 1;
            string snamhoc = "";
            string shocky = "";
            int sthang = date.Month;
            if (sthang >= 4 && sthang <= 7)
            {
                snamhoc = Convert.ToString(snamhoc2).Trim();
                shocky = "1";
            }
            else
            {
                snamhoc = Convert.ToString(snamhoc1).Trim();
                shocky = "2";
            }
            
            int n = Convert.ToInt32(col["soluongmh"]);
            bool ktranull = false;
            for (int i = 1; i <= n; i++)
            {
                //string smamhgv = col["gvchon" + i].Trim();
                if (col["gvchon" + i] == null)
                {
                    ktranull = true;
                    break;
                }
            }
            if (ktranull == false)
            {

                for (int i = 1; i <= n; i++)
                {
                    string smamhgv = col["gvchon" + i].Trim();
                    int vttru = smamhgv.IndexOf("-");
                    int dodai = smamhgv.Length;
                    string sgvchon = smamhgv.Substring(0, vttru);
                    string smamh = smamhgv.Substring(vttru + 1);
                    //var dkup1 = from d in db.dangkymonhocs
                    //           where d.masv.Trim() == smasv.Trim() & d.namhoc.Trim() == snamhoc & d.hocky.Trim() == shocky & d.mamh.Trim() == smamh.Trim()
                    //           select d;
                    var dkup = db.dangkymonhocs.Where(d => d.masv.Trim().Equals(smasv.Trim()) && d.namhoc.Trim().Equals(snamhoc) && d.hocky.Trim().Equals(shocky) && d.mamh.Trim().Equals(smamh.Trim())).FirstOrDefault();
                    if (dkup!=null)
                    {
                        dkup.magv = sgvchon;
                        UpdateModel(dkup);
                        db.SaveChanges();
                    }
                }
                return RedirectToAction("Ketquadangky");
            }
            else
                return RedirectToAction("Chonlai");
        }

    }
}
